#include <wfcClient.h>
#include <wxyutils.h>
#include <ExcelFormat.h>
#include <main.h>
namespace BE = ExcelFormat;
xstringsequence_ptr GenCellTexts(BE::BasicExcelWorksheet *ws, int r, int c);
wfcStatus ReadTable()
{
    if (CheckEnv(1, pfcMDL_DRAWING) == 0)
    {
        return wfcTK_NO_ERROR;
    }
    string DllPath;
    GetDllPath(DllPath);
    string xlspath = DllPath + "text/table_info.xls";
    try
    {
        pfcSession_ptr Session = pfcGetProESession();
        pfcModel_ptr Model = Session->GetCurrentModel();
        pfcDrawing_ptr Drawing = pfcDrawing::cast(Model);
        //选择一个表格
        pfcSelectionOptions_ptr SelectionOpts = pfcSelectionOptions::Create("dwg_table");
        SelectionOpts->SetMaxNumSels(1);
        pfcSelections_ptr Selections = Session->Select(SelectionOpts);
        if (Selections == NULL)
            return wfcTK_NO_ERROR;
        pfcTable_ptr Table = pfcTable::cast(Selections->get(0)->GetSelItem());
        xint ColCount = Table->GetColumnCount();
        xint RowCount = Table->GetRowCount();
        BE::BasicExcel wb;
        wb.New(1);                                        //初始化一个workbook
        BE::BasicExcelWorksheet *ws = wb.GetWorksheet(0); //获取第一个worksheet

        for (xint r = 0; r < RowCount; r++)
        {
            for (xint c = 0; c < ColCount; c++)
            {
                pfcTableCell_ptr TableCell = pfcTableCell::Create(r + 1, c + 1);
                xstring text;
                try
                {
                    //获取单元格内容
                    xstringsequence_ptr texts = Table->GetText(TableCell, pfcDWGTABLE_NORMAL);
                    text = texts->get(0);
                }
                xcatchbegin
                    xcatch(pfcXToolkitError, e) { text = " "; }
                xcatchend;
                ws->Cell(r, c)->Set(text.operator cWStringT()); //写入Unicode字符串
            }
        }
        wb.SaveAs(xlspath.c_str());
        return wfcTK_NO_ERROR;
    }
    OTK_EXCEPTION_PRINT_LOG
    catch (exception e)
    {
        LOG(LOG_ERROR) << "Fill in Table Failed! Error is " << e.what();
    }
    return wfcTK_NO_ERROR;
}
wfcStatus ChangeTable()
{
    string DllPath;
    GetDllPath(DllPath);
    pfcSession_ptr Session = pfcGetProESession();
    pfcModel_ptr Model = Session->GetCurrentModel();
    pfcDrawing_ptr Drawing = pfcDrawing::cast(Model);
    try
    {
        pfcFileOpenOptions_ptr FileOpenOpts = pfcFileOpenOptions::Create("*.xls,*.xlsx");
        xstring filepath = Session->UIOpenFile(FileOpenOpts);
        pfcSelectionOptions_ptr SelectionOpts = pfcSelectionOptions::Create("dwg_table");
        SelectionOpts->SetMaxNumSels(1);
        pfcSelections_ptr Selections = Session->Select(SelectionOpts);
        if (Selections == NULL)
            return wfcTK_NO_ERROR;
        pfcTable_ptr Table = pfcTable::cast(Selections->get(0)->GetSelItem());
        BE::BasicExcel wb;
        if (wb.Load(filepath.operator cStringT()) == false) //打开excel文件，文件被占用时load返回值为false
        {
            Session->UIDisplayMessage(ERRFILE, "File is opened!", NULL);
            return wfcTK_NO_ERROR;
        }
        BE::BasicExcelWorksheet *ws = wb.GetWorksheet(0);
        int rcont, ccont;
        rcont = ws->GetTotalRows();
        ccont = ws->GetTotalCols();

        int trcont, tccont;
        trcont = Table->GetRowCount();
        tccont = Table->GetColumnCount();
        pfcTableCell_ptr Cell = pfcTableCell::Create(trcont, tccont);
        //增加部分添加新行新列，行高1.5字符，列宽根据内容确定
        if (rcont > trcont)
        {
            for (int i = 0; i < (rcont - trcont); i++)
            {
                Table->InsertRow(1.5, trcont + i, true);
            }
        }
        if (ccont > tccont)
        {
            for (int j = 0; j < (ccont - tccont); j++)
            {
                int cwidth = 1;
                for (int r = 0; r < rcont; r++)
                {
                    BE::BasicExcelCell *wc = ws->Cell(r, tccont + j);
                    int tmpwidth = wc->GetStringLength();
                    if (tmpwidth > cwidth)
                        cwidth = tmpwidth;
                }
                Table->InsertColumn(cwidth, tccont + j, true);
            }
        }
        for (int r = 0; r < rcont; r++)
        {
            for (int c = 0; c < ccont; c++)
            {
                pfcTableCell_ptr TableCell = pfcTableCell::Create(r + 1, c + 1);
                xstringsequence_ptr CellTexts = GenCellTexts(ws, r, c);
                if (CellTexts->getarraysize() > 0)
                {
                    Table->SetText(TableCell, CellTexts);
                }
            }
        }
    }
    OTK_EXCEPTION_PRINT_LOG
    catch (exception e)
    {
        LOG(LOG_ERROR) << "Error is " << e.what();
    }
    Drawing->Regenerate();
    return wfcTK_NO_ERROR;
}
xstringsequence_ptr GenCellTexts(BE::BasicExcelWorksheet *ws, int r, int c)
{
    xstringsequence_ptr CellTexts = xstringsequence::create();
    BE::BasicExcelCell *wc = ws->Cell(r, c);
    if (wc->Type() == BE::BasicExcelCell::WSTRING)
    {
        const wchar_t *text = wc->GetWString();
        xstring xrstr(text);
        CellTexts->append(xrstr);
    }
    else if (wc->Type() == BE::BasicExcelCell::STRING)
    {
        const char *text = wc->GetString();
        xstring xrstr(text);
        CellTexts->append(xrstr);
    }
    else if (wc->Type() == BE::BasicExcelCell::INT)
    {
        int value = wc->GetInteger();
        stringstream text;
        text << value;
        xstring xrstr(text);
        CellTexts->append(xrstr);
    }
    else if (wc->Type() == BE::BasicExcelCell::DOUBLE)
    {
        double value = wc->GetDouble();
        stringstream text;
        text << value;
        xstring xrstr(text);
        CellTexts->append(xrstr);
    }
    return CellTexts;
}